package com.xysd.express.base;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;

import com.xysd.express.bean.User;
import com.xysd.express.widget.BaseSwipeRefreshLayout;


/**
 * Created by donghui on 2017/8/7.
 */

public class BaseFragment extends Fragment {

    protected BaseActivity mActivity;
    protected User user;
    Intent startAtyIntent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity=(BaseActivity) getActivity();
        user=User.getInstance();
    }


    protected void startAty(Class aty){
        if (startAtyIntent ==null)
            startAtyIntent =new Intent();
        startAtyIntent.setClass(mActivity,aty);
        mActivity.startActivity(startAtyIntent);
    }


    BaseSwipeRefreshLayout.OnRefresh refresh;//设置下拉刷新监听
    protected void setOnRefreshListener(BaseSwipeRefreshLayout.OnRefresh refresh){
        if (refresh!=null){
            this.refresh=refresh;
        }
    }
    boolean isRefresh;//是否在下拉刷新中

    /**
     * 初始化下拉刷新控件
     * @param swiperefreshlayout
     */
    protected void setSwipeRefreshLayout(final BaseSwipeRefreshLayout swiperefreshlayout) {
        swiperefreshlayout.setColorSchemeColors(Color.BLUE,
                Color.GREEN,
                Color.YELLOW,
                Color.RED);


        // 设置手指在屏幕下拉多少距离会触发下拉刷新
        swiperefreshlayout.setDistanceToTriggerSync(300);
        // 设定下拉圆圈的背景
        swiperefreshlayout.setProgressBackgroundColorSchemeColor(Color.WHITE);
        // 设置圆圈的大小
        swiperefreshlayout.setSize(SwipeRefreshLayout.LARGE);

        //设置下拉刷新的监听
        swiperefreshlayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //检查是否处于刷新状态
                if (!isRefresh) {
                    isRefresh = true;
                    //模拟加载网络数据，这里设置4秒，正好能看到4色进度条

                    //是否隐藏刷新进度条
                    swiperefreshlayout.setRefreshing(false);
                    //刷新页面数据
                    refresh.refresh();
                    //刷新完成重置刷新状态为false
                    isRefresh = false;
                }

            }
        });

    }

}
